package org.monster.boot;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * dos容器
 *
 * @author Monster
 */
public class DosContainer implements Container {

    /**
     * 日志器
     */
    private static final Logger logger = Logger.getLogger(DosContainer.class.getName());
    /**
     * dos命令队列
     */
    private static final LinkedBlockingQueue<String> DOS_COMMANDS = new LinkedBlockingQueue<>();
    /**
     * 容器静态
     * 单例
     */
    private static DosContainer container = null;

    /**
     * 默认构造方法
     */
    private DosContainer() {
    }

    /**
     * 获取实例
     * 获取DosContainer对象的单例
     *
     * @return 容器
     */
    public static Container getInstance() {
        synchronized (DosContainer.class) {
            if (container == null)
                container = new DosContainer();
        }
        return container;
    }

    /**
     * 放入
     * 将dos命令存入容器
     *
     * @param dosCommand dos命令
     */
    @Override
    public synchronized void put(String dosCommand) {
        try {
            DOS_COMMANDS.put(dosCommand);

            if (DOS_COMMANDS.size() > 0) {
                this.notifyAll();
                this.wait();
            }
        } catch (InterruptedException ex) {
            logger.log(Level.SEVERE, null, ex);
        }

    }

    /**
     * 获取
     * 将dos命令从容器移除
     *
     * @return {@link String} dos命令
     * @throws InterruptedException 终端异常
     */
    public synchronized String get() {
        try {
            if (DOS_COMMANDS.size() == 0) {
                this.notifyAll();
                this.wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return DOS_COMMANDS.remove();
    }

    /**
     * 大小
     *
     * @return {@link Integer} 大小
     */
    public int size() {
        return DOS_COMMANDS.size();
    }
}
